var coinChange = function (coins, amount) {
  let res = 0, rets = []
  coins = coins.sort();
  for (let i = coins.length; i >= 0; i--) {
    let coin = coins[i]
    //从大到小试
    while (res + coin <= amount) {
      rets.push(coin)
      res += coin
    }
  }
  if (sum(rets) === amount) {
    return rets.length
  }
  return -1;
};

function sum(rets) {
  return rets.reduce((prev, curr) => prev + curr, 0)
}

console.log(coinChange([2], 3));
